//
//  ViewController.m
//  JSON_FORMAT
//
//  Created by ms on 2023/1/28.
//

#import "ViewController.h"
#import "NSObject+MSObject.h"

@interface ViewController ()

@property (strong) IBOutlet NSTextField *textView;
@property (strong) IBOutlet NSTextField *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    
}

- (IBAction)format:(id)sender {
    
    NSDictionary *dict = [self.textView.stringValue ms_JSONObject];
    if ([dict isKindOfClass:[NSArray class]])
    {
        dict = ((NSArray *)dict).firstObject;
    }
    
    NSMutableString *strM = [NSMutableString string];
    
    [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {\
        
        NSString *str;
        
        if ([NSStringFromClass([obj class]) containsString:@"String"])
        {
           str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSString *%@;", key];
        }
        if ([NSStringFromClass([obj class]) containsString:@"Number"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, strong) NSNumber *%@;", key];
        }
        if ([NSStringFromClass([obj class]) containsString:@"Array"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSArray *%@;", key];
        }
        if ([NSStringFromClass([obj class]) containsString:@"Dictionary"])
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, copy) NSDictionary *%@;", key];
        }
        if ([NSStringFromClass([obj class]) containsString:@"Boolean"])\
        {
            str = [NSString stringWithFormat:@"/** <#remark#> */\n@property (nonatomic, assign) BOOL %@;", key];
        }
        [strM appendFormat:@"\n%@",str];
    }];
    
    self.textLabel.stringValue = strM;
}


/** 转化为数组或字典 */
- (id)ms_JSONObject
{
    if ([self isKindOfClass:[NSString class]])
    {
        return [NSJSONSerialization JSONObjectWithData:[((NSString *)self) dataUsingEncoding:NSUTF8StringEncoding]
                                               options:kNilOptions
                                                 error:nil];
    }
    else if ([self isKindOfClass:[NSData class]])
    {
        return [NSJSONSerialization JSONObjectWithData:(NSData *)self
                                               options:kNilOptions
                                                 error:nil];
    }
    
    return self;
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}


@end
